package com.xoa.service.party;


import com.xoa.dao.party.*;
import com.xoa.model.party.*;
import com.xoa.util.common.wrapper.BaseWrapper;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Service
public class FileUploadService {
    @Autowired
    EsCommitteeMapper esCommitteeMapper;
    @Autowired
    EsCarportMapper esCarportMapper;
    @Autowired
    EsGroupControlMapper esGroupControlMapper;
    @Autowired
    EsPoliceStationMapper esPoliceStationMapper;
    @Autowired
    EsDisplayMapper esDisplayMapper;
    @Autowired
    EsMonitorSystemMapper esMonitorSystemMapper;
    @Autowired
    EsPropertyCompanyMapper esPropertyCompanyMapper;
    @Autowired
    EsSubstationMapper esSubstationMapper;
    @Autowired
    MapOwnerinfoMapper mapOwnerinfoMapper;
    public BaseWrapper fileUpload(MultipartFile file){
        BaseWrapper baseWrapper=new BaseWrapper();
        try {
            if (file.isEmpty()) {
                baseWrapper.setMsg("请上传文件！");
                baseWrapper.setFlag(false);
                return baseWrapper;
            }else {
                String fileName = file.getOriginalFilename();
                InputStream in =  file.getInputStream();
                HSSFWorkbook excelObj = new HSSFWorkbook(in);
                HSSFSheet sheetObj = excelObj.getSheetAt(0);
                Row row = sheetObj.getRow(0);
                int colNum = row.getPhysicalNumberOfCells();
                int lastRowNum = sheetObj.getLastRowNum();
                int insertConut=0;
                int updateConut=0;
                Integer counsum[] =new Integer[2];
                Map map=new HashMap<String,Object>();
                for(int i=1;i<=lastRowNum;i++){
                    EsCommittee esCommittee=new EsCommittee();//社区居委会
                    EsCarport esCarport=new EsCarport();//车棚
                    EsGroupControl esGroupControl=new EsGroupControl();//群房布控点位
                    EsPoliceStation esPoliceStation=new EsPoliceStation();//巡逻警务点
                    EsDisplay esDisplay=new EsDisplay();//电子显示屏
                    EsMonitorSystem esMonitorSystem=new EsMonitorSystem();//频监控系统
                    EsPropertyCompany esPropertyCompany=new EsPropertyCompany();//物业公司
                    EsSubstation esSubstation=new EsSubstation();//变电站
                    row=sheetObj.getRow(i);
                    if(row!=null){
                        Cell c0 = row.getCell(0); //分类（1是社区居委会2车棚3群房布控点位4巡逻警务点5电子显示屏6视频监控系统7物业公司8变电站）
                        Cell c1 = row.getCell(1); //具体位置
                        Cell c2 = row.getCell(2); //负责人
                        Cell c3 = row.getCell(3); //主管部门
                        Cell c4 = row.getCell(4); //联系方式
                        Cell c5 = row.getCell(5); //创建人
                        Cell c6 = row.getCell(6); //创建时间
                        Cell c7 = row.getCell(7); //位置
                        if(c0!=null){
                            row.getCell(0).setCellType(CellType.STRING);
                            row.getCell(1).setCellType(CellType.STRING);
                            row.getCell(2).setCellType(CellType.STRING);
                            row.getCell(3).setCellType(CellType.STRING);
                            row.getCell(4).setCellType(CellType.STRING);
                            if(c5!=null){
                                row.getCell(5).setCellType(CellType.STRING);
                            }
                            if(c6!=null){
                                row.getCell(6).setCellType(CellType.STRING);
                            }
                            if(c7!=null){
                                row.getCell(7).setCellType(CellType.STRING);
                            }

                            if("1".equals(c0.getStringCellValue().trim())){
                                esCommittee.setLocation(c1.getStringCellValue());
                                esCommittee.setResponsible(c2.getStringCellValue());
                                esCommittee.setDirectorDept(c3.getStringCellValue());
                                esCommittee.setContact(c4.getStringCellValue());
                                esCommittee.setCreateUser(c5.getStringCellValue());
                                esCommittee.setCreateTime(c6.getStringCellValue());
                                esCommittee.setC1(mapOwnerinfoMapper.selectCoordingbyName(c7.getStringCellValue()));
                                esCommitteeMapper.insertSelective(esCommittee);
                            }else if ("2".equals(c0.getStringCellValue().trim())){
                                esCarport.setLocation(c1.getStringCellValue());
                                esCarport.setResponsible(c2.getStringCellValue());
                                esCarport.setDirestorDept(c3.getStringCellValue());
                                esCarport.setContact(c4.getStringCellValue());
                                esCarport.setCreateUser(c5.getStringCellValue());
                                esCarport.setCreateTime(c6.getStringCellValue());
                                esCarport.setP1(mapOwnerinfoMapper.selectCoordingbyName(c7.getStringCellValue()));
                                esCarportMapper.insertSelective(esCarport);
                            }else if ("3".equals(c0.getStringCellValue().trim())){
                                esGroupControl.setLocation(c1.getStringCellValue());
                                esGroupControl.setResponsible(c2.getStringCellValue());
                                esGroupControl.setDirectorDept(c3.getStringCellValue());
                                esGroupControl.setContact(c4.getStringCellValue());
                                esGroupControl.setCreateUser(c5.getStringCellValue());
                                esGroupControl.setCreateTime(c6.getStringCellValue());
                                esGroupControl.setT1(mapOwnerinfoMapper.selectCoordingbyName(c7.getStringCellValue()));
                                esGroupControlMapper.insertSelective(esGroupControl);
                            }else if ("4".equals(c0.getStringCellValue().trim())){
                                esPoliceStation.setLocation(c1.getStringCellValue());
                                esPoliceStation.setResponsible(c2.getStringCellValue());
                                esPoliceStation.setDirectorDept(c3.getStringCellValue());
                                esPoliceStation.setContact(c4.getStringCellValue());
                                esPoliceStation.setCreateUser(c5.getStringCellValue());
                                esPoliceStation.setCreateTime(c6.getStringCellValue());
                                esPoliceStation.setS1(mapOwnerinfoMapper.selectCoordingbyName(c7.getStringCellValue()));
                                esPoliceStationMapper.insertSelective(esPoliceStation);
                            }else if ("5".equals(c0.getStringCellValue().trim())){
                                esDisplay.setLocation(c1.getStringCellValue());
                                esDisplay.setResponsible(c2.getStringCellValue());
                                esDisplay.setDirectorDept(c3.getStringCellValue());
                                esDisplay.setContact(c4.getStringCellValue());
                                esDisplay.setCreateUser(c5.getStringCellValue());
                                esDisplay.setCreateTime(c6.getStringCellValue());
                                esDisplay.setE1(mapOwnerinfoMapper.selectCoordingbyName(c7.getStringCellValue()));
                                esDisplayMapper.insertSelective(esDisplay);
                            }else if ("6".equals(c0.getStringCellValue().trim())){
                                esMonitorSystem.setLocation(c1.getStringCellValue());
                                esMonitorSystem.setResponsible(c2.getStringCellValue());
                                esMonitorSystem.setDirectorDept(c3.getStringCellValue());
                                esMonitorSystem.setContact(c4.getStringCellValue());
                                esMonitorSystem.setCreateUser(c5.getStringCellValue());
                                esMonitorSystem.setCreateTime(c6.getStringCellValue());
                                esMonitorSystem.setM1(mapOwnerinfoMapper.selectCoordingbyName(c7.getStringCellValue()));
                                esMonitorSystemMapper.insertSelective(esMonitorSystem);
                            }else if ("7".equals(c0.getStringCellValue().trim())){
                                esPropertyCompany.setLocation(c1.getStringCellValue());
                                esPropertyCompany.setResponsible(c2.getStringCellValue());
                                esPropertyCompany.setDirectorDept(c3.getStringCellValue());
                                esPropertyCompany.setContact(c4.getStringCellValue());
                                esPropertyCompany.setCreateUser(c5.getStringCellValue());
                                esPropertyCompany.setCreateTime(c6.getStringCellValue());
                                esPropertyCompany.setP1(mapOwnerinfoMapper.selectCoordingbyName(c7.getStringCellValue()));
                                esPropertyCompanyMapper.insertSelective(esPropertyCompany);
                            }else if ("8".equals(c0.getStringCellValue().trim())){
                                esSubstation.setLocation(c1.getStringCellValue());
                                esSubstation.setResponsible(c2.getStringCellValue());
                                esSubstation.setDirectorDept(c3.getStringCellValue());
                                esSubstation.setContact(c4.getStringCellValue());
                                esSubstation.setCreateUser(c5.getStringCellValue());
                                esSubstation.setCreateTime(c6.getStringCellValue());
                                esSubstation.setB1(mapOwnerinfoMapper.selectCoordingbyName(c7.getStringCellValue()));
                                esSubstationMapper.insertSelective(esSubstation);
                            }


                            baseWrapper.setStatus(true);
                            baseWrapper.setFlag(true);
                            baseWrapper.setMsg("ok");

                        }

                    }
                }
            }
        }catch (Exception e){
        e.printStackTrace();
        }
        return  baseWrapper;
    }
}
